/* 
 * AndFollowerStat
 * 
 * Copyright (C) 2009  Daniel Czerwonk <d.czerwonk@googlemail.com>
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package de.dan_nrw.android.followerstat;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.TextView;

import de.dan_nrw.android.followerstat.client.configuration.ConfigActivity;
import de.dan_nrw.android.followerstat.client.followers.FollowerListActivity;
import de.dan_nrw.android.followerstat.dao.configuration.IConfigDAO;
import de.dan_nrw.android.followerstat.widget.UpdateFollowerStatWidgetService;


/**
 * @author Daniel Czerwonk
 *
 */
public class AndroidFollowerStatActivity extends Activity {

	private final IConfigDAO configDAO;
	
	private static final int DIALOG_ABOUT = 0;
	private static final int ACTIVITY_SETTINGS = 0;
	

	/**
	 * Creates a new instance of AndroidFollowerStatActivity.
	 */
	public AndroidFollowerStatActivity() {
	    super();
	    
	    DependencyInjector.init(this);
	    
	    this.configDAO = DependencyInjector.getInstance(IConfigDAO.class);
    }


    /* (non-Javadoc)
     * @see android.app.Activity#onCreate(android.os.Bundle)
     */
    @Override
    protected void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    
	    this.setContentView(R.layout.main);
	    
	    this.updateUserName();
    }

    /* (non-Javadoc)
     * @see android.app.Activity#onCreateDialog(int)
     */
    @Override
    protected Dialog onCreateDialog(int id) {
	    switch (id) {
	    	case DIALOG_ABOUT:
	    		return new AboutDialog(this);
	    }
	    
	    return null;
    }

	/* (non-Javadoc)
     * @see android.app.Activity#onActivityResult(int, int, android.content.Intent)
     */
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
	    super.onActivityResult(requestCode, resultCode, data);
	    
	    if (requestCode == ACTIVITY_SETTINGS
	    		&& resultCode == ConfigActivity.RETURN_CODE_OK) {
	    	this.updateUserName();
	    	
	    	this.updateWidget();
	    }
    }
    
	private void updateUserName() {
    	TextView textView = (TextView)this.findViewById(R.id.mainTextView);
    	
    	if (this.configDAO.getTwitterUserName().equals("")) {   

    		textView.setText(this.getString(R.string.noUserNameDefinedErrorText));
    		textView.setBackgroundColor(R.color.ErrorColor);
    		
    		return;
    	}
    	
    	String userName = this.configDAO.getTwitterUserName();
    	
    	textView.setText(userName);
    	textView.setBackgroundColor(android.R.color.black);
    }

	/* (non-Javadoc)
     * @see android.app.Activity#onCreateOptionsMenu(android.view.Menu)
     */
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
	    MenuInflater menuInflater = new MenuInflater(this);
	    
	    menuInflater.inflate(R.menu.menu, menu);
    	
    	return true;
    }

	/* (non-Javadoc)
     * @see android.app.Activity#onOptionsItemSelected(android.view.MenuItem)
     */
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
	    switch (item.getItemId()) {
	    	case R.id.aboutMenuItem:
	    		this.showDialog(DIALOG_ABOUT);
	    		return true;
	    		
	    	case R.id.followersMenuItem:
	        	this.startActivity(new Intent(this, FollowerListActivity.class));
	    		return true;
	    		
	    	case R.id.settingsMenuItem:
	    		this.startActivityForResult(new Intent(this, ConfigActivity.class), ACTIVITY_SETTINGS);
	    		return true;
	    		
	    	case R.id.closeMenuItem:
	    		this.finish();
	    		return true;
	    		
	    	default:
	    		return false;
	    }
    }
    
	private void updateWidget() {
		Intent intent = new Intent(this.getApplicationContext(), UpdateFollowerStatWidgetService.class);
		
		intent.putExtra(UpdateFollowerStatWidgetService.TWITTER_USERNAME, this.configDAO.getTwitterUserName());
		
		this.startService(intent);
    }
}